package services

import (
	"os"
	"path/filepath"
)

type FileService struct {
}

func NewFileService() *FileService {
	return &FileService{}
}

func (s *FileService) GetSupportedVideoFormats() []string {
	return []string{
		".mp4",
		".avi",
		".mkv",
		".mov",
		".wmv",
		".flv",
		".webm",
		".m4v",
		".3gp",
		".ogv",
	}
}

func (s *FileService) IsVideoFile(filename string) bool {
	ext := filepath.Ext(filename)
	supported := s.GetSupportedVideoFormats()
	
	for _, format := range supported {
		if ext == format {
			return true
		}
	}
	return false
}

func (s *FileService) GetVideoInfo(path string) (map[string]interface{}, error) {
	fileInfo, err := os.Stat(path)
	if err != nil {
		return nil, err
	}

	info := map[string]interface{}{
		"name":     fileInfo.Name(),
		"size":     fileInfo.Size(),
		"modified": fileInfo.ModTime(),
		"path":     path,
	}

	return info, nil
}

func (s *FileService) ScanDirectory(dirPath string) ([]map[string]interface{}, error) {
	var videos []map[string]interface{}
	
	err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		
		if !info.IsDir() && s.IsVideoFile(info.Name()) {
			videoInfo, err := s.GetVideoInfo(path)
			if err == nil {
				videos = append(videos, videoInfo)
			}
		}
		
		return nil
	})
	
	return videos, err
}
